import { defineStore } from "pinia";
import type { reqCategoryReturnType } from "@/api/attr"
import {
    requestCategory1,
    requestCategory2,
    requestCategory3
}from "@/api/attr"
import { ElMessage } from "element-plus";

//定义state的类型
export interface attrStoreType {
    catecoryList1: reqCategoryReturnType,
    catecoryList2: reqCategoryReturnType,
    catecoryList3: reqCategoryReturnType,
    category1Id: number | null
    category2Id: number | null
    category3Id: number | null
}
//创建保存三级列表数据的仓库
const useAttrStore = defineStore("attr", {
    state: (): attrStoreType => {
        return {
            catecoryList1: [],
            catecoryList2: [],
            catecoryList3: [],
            category1Id: null,
            category2Id: null,
            category3Id: null,
        }
    },
    actions: {
        //1.请求一级列表
        async getCategory1(){
            try {
                const result = await requestCategory1()
                this.catecoryList1 = result
            } catch (error) {
                ElMessage.error("请求一级列表失败")
            }
        },
        //2.请求二级列表
        async getCategory2(){
            try {
                const result = await requestCategory2(this.category1Id as  number)
                this.catecoryList2 = result
            } catch (error) {
                ElMessage.error("请求二级列表失败")
            }
        },
        //3.请求三级列表
        async getCategory3(){
           try {
                const result = await requestCategory3(this.category2Id as  number)
                this.catecoryList3 = result
           } catch (error) {
                ElMessage.error("请求三级列表失败")
           }
        }
    },
    getters: {},

})

export default useAttrStore